package com.geo.geotool.util.file;

import sun.misc.BASE64Decoder;

import java.io.*;

/**
 * • @Description: base64 与 file 互转
 * • @author  : yell
 * • @date : 2021/11/24 15:48
 */
public class Base64File {
    /**生成file*/
    public static boolean generateFle(String base64FileStr,File file){
        if(base64FileStr == null){
            return false;
        }
        BASE64Decoder decoder = new BASE64Decoder();
        OutputStream out = null;
        try{
            byte[] byt = decoder.decodeBuffer(base64FileStr);
            for(int i = 0 , len = byt.length ; i < len ; ++i){
                if(byt[i] < 0 ){
                    byt[i] +=256;
                }
            }
            InputStream input = new ByteArrayInputStream(byt);
            out = new FileOutputStream(file);
            byte[] buff = new byte[1024];
            int len ;
            while ((len=input.read(buff))!=-1){
                out.write(buff,0,len);
            }
        }catch (IOException e){
            e.printStackTrace();
        }finally {
            try{
                out.flush();
                out.close();
            }catch (Exception e){
                e.printStackTrace();
            }
        }
        return true;
    }


}
